﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLNSTL
{
    public partial class dialogThemNV : Form
    {
        public dialogThemNV()
        {
            InitializeComponent();
        }
        ketnoi cnn = new ketnoi();
        private void dialogThemNV_Load(object sender, EventArgs e)
        {
            LamTuoi();
            LoadCBBPB();
            LoadCBBTD();
            this.CenterToParent();
        }
        private void LoadCBBPB()
        {
            try
            {
                cbbPB.DataSource = cnn.getData("PRCSelectAllPHONGBAN");
                cbbPB.DisplayMember = "TENPB";
                cbbPB.ValueMember = "MAPB";
                cbbPB.Items.Clear();

            }
            catch (Exception)
            {
                return;
            }
        }
        private void LoadCBBTD()
        {
            try
            {
                cbbTD.DataSource = cnn.getData("PRCSelectAllTRINHDO");
                cbbTD.DisplayMember = "TENTD";
                cbbTD.ValueMember = "MATD";
                cbbTD.Items.Clear();
                
            }
            catch (Exception)
            {
                return;
            }
        }
        private void LamTuoi() 
        {
            txtHoten.Text = "";
            cbbGioiTinh.Text = "";
            deNamSinh.Text = "";
            txtCMND.Text = "";
            txtSDT.Text = "";
            deNGAYVLV.Text = "";
            cbbPB.Text = "";
            cbbTD.Text = "";
            txtHoten.Focus();
        }
        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtHoten.Text == "" ||
                deNamSinh.Text == "" ||
                txtCMND.Text == "" ||
                txtSDT.Text == "" ||
                deNGAYVLV.Text == "" ||
                cbbPB.Text==""||
                cbbTD.Text==""

                )
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin!");
                if (txtHoten.Text == "")
                {
                    txtHoten.Focus();
                }
                else if (deNamSinh.Text == "")
                {
                    deNamSinh.Focus();
                }
                else if (txtCMND.Text == "")
                {
                    txtCMND.Focus();
                }
                else if (txtSDT.Text == "")
                {
                    txtSDT.Focus();
                }
                else if (deNGAYVLV.Text == "")
                {
                    deNGAYVLV.Focus();
                }
                else if (cbbPB.Text == "")
                {
                    txtHoten.Focus();
                }
                else if (cbbTD.Text == "")
                {
                    txtHoten.Focus();
                }

            }
            else
            {
                int ts = 8;
                string[] name = new string[ts];
                object[] value = new object[ts];
                name[0] = "@MATD";
                name[1] = "@MAPB";
                name[2] = "@TENNV";
                name[3] = "@GIOITINH";
                name[4] = "@NAMSINH";
                name[5] = "@CMND";
                name[6] = "@SDT";
                name[7] = "@NGAYVLV";
                
                value[0] = cbbTD.SelectedValue.ToString();
                value[1] = cbbPB.SelectedValue.ToString();
                value[2] = txtHoten.Text.Trim();
                value[3] = cbbGioiTinh.Items[cbbGioiTinh.SelectedIndex].ToString();
                value[4] = Convert.ToDateTime(deNamSinh.Text.Trim());
                value[5] = txtCMND.Text.Trim();
                value[6] = txtSDT.Text.Trim();
                value[7] = Convert.ToDateTime(deNGAYVLV.Text.Trim());
                
                int ck = cnn.Update("PRCInsertNHANVIEN", name, value, ts);
                if (ck > 0)
                {
                   // MessageBox.Show("Thêm mới thành công");
                    this.Close();
                }
                else
                {
                   // MessageBox.Show("Thêm mới thất bại");
                    dialogThemNV_Load(sender, e);
                }

            }
        }
    }
}
